/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package assisisw.smartcal.webuisubsystem.managedbean.event;

import assisisw.smartcal.entitysubsystem.Event;
import assisisw.smartcal.entitysubsystem.IEventController;
import assisisw.smartcal.entitysubsystem.User;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedList;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.inject.Named;
import org.primefaces.event.SelectEvent;

/**
 *
 * @author Francesco
 */
@ManagedBean ( eager = true )
@Named ( "newEventControllerBean" )
@ViewScoped ()
public class NewEventControllerBean extends AbstractEventControllerBean
{
    
    private Collection < User > usersToInvite ;
    
    // Instance Methods
    
    /***/
    public NewEventControllerBean () {}

    /**
     * AS THE SUPER'S ONE.
     */
    @Override
    public void confirmAction () 
    {
        addEvent();
    }
    
    /**
     * AS THE SUPER'S ONE.
     */
    @Override
    public void undoAction() {}
    
    /**
     * @param s
     */
    public void setNewManagedEvent ( SelectEvent s ) 
    {
        Event e ;
        e = new Event ();
        e.setIniDate((Date)s.getObject());
        e.setEndDate((Date)s.getObject());
        setManagedEvent ( e ) ;
        setWeatherInfoAvailable ( false ) ;
        setAvailableCalendars ( getDashboardBean ().getManagedUser ().getOwnedCalendars () ) ;
        setEditAllowed ( true ) ;
        usersToInvite = new LinkedList <> () ;
        setTimeConsistencyInfo(""); 
    }
    
    /***/
    private void addEvent () 
    {
        IEventController eventController ;
        Event event ;
        event = getManagedEvent () ;
        eventController = getEventController () ;        
        eventController.insert ( event , getSelectedCalendar () ) ;
        for ( User u : usersToInvite )
            eventController.invite(event, u); 
        getDashboardBean ().onEventAdded ( event ) ;
    }

    @Override
    protected void effectiveSharing(User u) 
    {
        usersToInvite.add(u);
    }
    
}
